<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>
<%@ include file="/home/include/init_home.jsp" %>
<%@ page import="com.qbooks.board.*" %>
<%
	String ip_addr			= request.getRemoteAddr();
	String word_id			= StringUtil.nvl(request.getParameter("word_id"));
	String search_col		= StringUtil.nvl(request.getParameter("search_col"), "subject");
	String search_word		= StringUtil.toHan(StringUtil.nvl(request.getParameter("search_word")));
	String curr_page 		= StringUtil.nvl(request.getParameter("curr_page"), "1");

	HashMap param = new HashMap();
	param.put("word_id",	word_id);


	BoardDAO boardDAO 		= new BoardDAO();
	HashList wordInfo		= boardDAO.selectWordInfo(param);
	HashList commentList	= null;


	if ("Y".equals(wordInfo.get("comment_yn",0))) {
		commentList = boardDAO.selectCommentList(param);
	}


	String board_id			= wordInfo.get("board_id",0);

	if (!ip_addr.equals(wordInfo.get("ip_addr",0))) {
		param.put("job_type", "UC");
		boardDAO.saveWord(param);
	}


	//쓰기권한
	boolean bWritable		= true;
	if ("A".equals(wordInfo.get("write_auth",0))) {	//관리자
		bWritable = isAdmin;
	} else if ("M".equals(wordInfo.get("write_auth",0))) {	//회원
		bWritable = isLogin;
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title><%= HOME_NAME %></title>
	<%@ include file="/home/include/cssnjs.jsp"  %>
	<script>
	function fnDelete() {
		if (confirm(MSG_DELETE)) {
			document.boardForm.submit();
		}
	}

	function fnWriteComment() {
		if (cfCheckLogin(<%=isLogin%>)) {
			if (document.commentForm.comment.value == "") {
				alert("내용을 입력하세요.");
				return;
			}
			document.commentForm.job_type.value = "C";
			document.commentForm.submit();
		}
	}

	function fnDeleteComment(commentId) {
		if (confirm(MSG_DELETE)) {
			document.commentForm.job_type.value = "D";
			document.commentForm.comment_id.value = commentId;
			document.commentForm.submit();
		}
	}

	</script>
</head>
<body>
<div id="wrap">
		<h3><%= wordInfo.get("board_name",0) %></h3>

		<form name="boardForm" action="./wordDelete.do" method="post">
        <input type="hidden" name="board_id" value="<%= board_id %>"/>
        <input type="hidden" name="word_id" value="<%= word_id %>"/>
		<div>제목 : <%= wordInfo.get("subject",0) %></div>
		<div>작성일 : <%= wordInfo.get("init_date",0) %></div>
		<div>작성자 : <%= wordInfo.get("author_name",0) %></div>
		<div>조회수 : <%= wordInfo.get("count",0) %></div>
		<div style="min-height:300px">
<%		if ("Y".equals(wordInfo.get("html_yn",0))) { %>
			<%= wordInfo.get("content",0) %>
<%		} else { %>
			<%= StringUtil.toHTML(wordInfo.get("content",0)) %>
<%		} %>
		</div>


		<div style="float:right; margin-bottom:30px">
<%		if (bWritable) {	%>
			<a href="./wordWrite.jsp?board_id=<%= board_id %>"><img src="../images/sub/write02.gif" border="0" alt="글쓰기"/></a>
<%		} %>
<%		if (bWritable && wordInfo.get("reply_yn",0).equals("Y")) {	%>
		  	<a href="./wordReply.jsp?word_id=<%= word_id %>&board_id=<%= board_id %>"><img src="../images/sub/reply02.gif" border="0" alt="답글"></a>
<%		}	%>
<%		if (isAdmin) { %>
		  	<a href="./wordUpdate.jsp?word_id=<%= word_id %>&board_id=<%= board_id %>"><img src="../images/sub/edit02.gif" border="0" alt="수정"></a>
		  	<a href="#nohref" onclick="fnDelete()"><img src="../images/sub/delete02.gif" border="0" alt="삭제"></a>
<%		} else if (isLogin && session_user_id.equals(wordInfo.get("init_user",0))) { %>
		  	<a href="./wordUpdate.jsp?word_id=<%= word_id %>&board_id=<%= board_id %>"><img src="../images/sub/edit02.gif" border="0" alt="수정"></a>
		  	<a href="#nohref" onclick="fnDelete()"><img src="../images/sub/delete02.gif" border="0" alt="삭제"></a>
<%		} %>
		  	<a href="./wordList.jsp?board_id=<%=board_id%>&search_col=<%=search_col%>&search_word=<%=search_word%>&curr_page=<%=curr_page%>"><img src="../images/sub/list_btn.gif" border="0" alt="목록보기" /></a>
		</div>
		</form>

<%	if ("Y".equals(wordInfo.get("comment_yn",0))) { %>

		<div id="comment" style="clear:both;">
			<form name="commentForm" action="./commentSave.do" method="post">
	        <input type="hidden" name="job_type" value="C"/>
	        <input type="hidden" name="board_id" value="<%= board_id %>"/>
	        <input type="hidden" name="word_id" value="<%= word_id %>"/>
	        <input type="hidden" name="comment_id" value=""/>

        	<div style="float:left; ">댓글</div>
        	<div style="margin-left:20px; font-size:9pt">총 <%= commentList.size() %>건</div>
        	<!-- <div style="text-align:right"><a href="#nohref" onclick="$('#comment_write').toggle()">덧글작성</a></div> -->

        	<hr style="clear:both"/>

        	<div id="comment_write">
  				<textarea name="comment" style="width:100%; height:80px;" onfocus="cfCheckLogin(<%=isLogin%>)"></textarea>
        		<input type="button" value="댓글등록" onclick="fnWriteComment()"/>
        	</div>

	<%	for (int i = 0; i < commentList.size(); i++) { %>
       		<div>
       			<%= commentList.get("author_name", i) %> &nbsp;
       			<small><%= commentList.get("init_date",i) %></small>
		<%	if (isAdmin || session_user_id.equals(commentList.get("init_user",i))) { %>
				<input type="button" value="댓글삭제"  onclick="fnDeleteComment('<%= commentList.get("comment_id", i)%>')"/>
		<%	} %>
       		</div>
       		<div>
       	<%	if ("Y".equals(commentList.get("html_yn",i))) { %>
       			<%= commentList.get("comment",i) %>
       	<%	} else { %>
       			<%= commentList.getToBr("comment",i) %>
       	<%	} %>
       		</div>
       		<hr/>
	<%	} %>
        	</form>
		</div>
<%	} %>


</div>
</body>
</html>
